package com.yl.order.utils;

import com.yl.order.App;
import com.yl.order.R;
import com.yl.order.db.entity.OrderEntity;
import com.yl.order.db.entity.OrderGoodsEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

/**
 * create by cjx on 2024/6/8
 * desc:
 */
public class OrderUtils {

    public static OrderEntity buildOrderEntity(String price) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        SimpleDateFormat orderFormat = new SimpleDateFormat("yyMMddHHmmssSSS", Locale.CHINA);
        return new OrderEntity(null,
                orderFormat.format(date),
                App.app.getString(R.string.order_origin_local), 0, 0, price, 0,
                null, date.getTime(), sdf.format(date));
    }

    public static ArrayList<OrderGoodsEntity> buildOrderDetail(OrderEntity order, ArrayList<OrderGoodsEntity> orderGoods) {
        ArrayList<OrderGoodsEntity> result = new ArrayList<>();
        int seq = 1;
        for (OrderGoodsEntity goods : orderGoods) {
            result.add(goods);
            goods.setOrderNo(order.getOrderNo());
            goods.setOrderSubNo(String.valueOf(seq++));
            goods.setUpdateTimeStamp(order.getUpdateTimestamp());
            goods.setUpdateTime(order.getUpdateTime());
            if (goods.getQuantity() > 1) {
                for (int i = 1; i < goods.getQuantity(); i++) {
                    OrderGoodsEntity newGoods = new OrderGoodsEntity(null, order.getOrderNo(),
                            String.valueOf(seq++), goods.getProductNumber(), goods.getGoodsName(),
                            goods.getGoodsNumber(), goods.getGoodsIcon(), goods.getSpecNumber(),
                            goods.getSpecName(), goods.getRemark(), goods.getPrice(), 1, 0,
                            order.getUpdateTimestamp(), order.getUpdateTime(), null, 0);
                    result.add(newGoods);
                }
                goods.setQuantity(1);
            }
        }
        return result;
    }
}
